package com.example.easyrulesimple.pdf;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;

public class HtmlGenerator {

    public static String generate(String template, Map<String, Object> variables) throws IOException, TemplateException, IOException {
     
        Configuration config = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
        // 指定FreeMarker模板文件的位置
        config.setClassForTemplateLoading(HtmlGenerator.class, "/filePath");
        //读取模板文件地址
        config.setDefaultEncoding("UTF-8");
        //获取模板文件
        Template tp = config.getTemplate(template);
        StringWriter stringWriter = new StringWriter();
        BufferedWriter writer = new BufferedWriter(stringWriter);
        tp.setEncoding("UTF-8");
        //把map数据写入
        tp.process(variables, writer);
        String htmlStr = stringWriter.toString();
        writer.flush();
        writer.close();
        return htmlStr;
    }
}
